package com.ruoyi.carShop.controller;

import com.ruoyi.carShop.service.IShopZiliaoService;
import com.ruoyi.carShop.service.LoadService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.Merchant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import com.ruoyi.system.api.domain.Merchant;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;
import static com.ruoyi.common.core.web.domain.AjaxResult.success;

@CrossOrigin
@RestController
@RequestMapping("/carShop")
public class ShopZiliaoController extends BaseController {
    @Autowired
    private IShopZiliaoService shopZiliao;
    @Autowired
    private LoadService loadService;
    @RequestMapping("/merchant/list")
    public List<Merchant> selectMerchantList(Long merchantId){
        //1 获取request对象
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        System.out.println(mId);
        return shopZiliao.selectMerchantList((long) mId);
    }
    @PostMapping("/ShopZiliao/update")
    public AjaxResult updateShopZiliao(@RequestBody Merchant merchant) throws IOException {
        //1 获取request对象
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        merchant.setMerchantId((long) mId);
        System.out.println( merchant.getDirector());
        System.out.println("Received merchant: " + merchant);
        return toAjax(shopZiliao.updateShopZiliao(merchant));
    }
}
